package lv.servidordisqueria.persistencia;

import java.io.File;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

/**
 * Esta es la clase de hibernate de creacion de sessiones de acceso a la base de datos. 
 */
public class HibernateUtil {

	private static SessionFactory sessionFactory = null;
	private static Session s = null;

	public static Session getSession() throws HibernateException {
		try {
			if (sessionFactory == null) {
				File file = new File("resources/hibernate.cfg.xml");
				sessionFactory = new AnnotationConfiguration().configure(file).
						buildSessionFactory();
				
				s =  sessionFactory.openSession();
			}
		} catch (Throwable ex) {
			// Log exception!
			ex.printStackTrace();
			throw new ExceptionInInitializerError(ex);
		}

		if(!s.isOpen()){
			s =  sessionFactory.openSession();
		}
		return s;

	}
}